package com.tarena.rocketmq.normal;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
@Slf4j
public class NormalProducer {
    /*
        编写一个rocketmq的生产者发送消息的代码 发送普通消息
        达到队列normal-topic
     */
    public static void main(String[] args) {
        //1.创建rocketmq默认生产者
        DefaultMQProducer producer = new DefaultMQProducer();
        //2.设置localhost:9876 设置分组normal-group-prod
        producer.setNamesrvAddr("localhost:9876");
        producer.setProducerGroup("normal-group-prod");
        //3.准备发送消息message 包含字符串body
        String body = "hello rocketmq";
        //4.启动生产者并且发送消息
        SendResult result = null;
        try {
            producer.start();
            for (int i=0;i<100;i++){
                String msg=body+" "+i;
                Message message=new Message("normal-topic",msg.getBytes());
                result = producer.send(message);
                log.info("发送消息成功，消息内容:{},结果是：{}", msg,result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
